{extend name="base" /}
<!-- page specific plugin styles -->
{block name="inline-styles"}
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page js_show">
    <div class="page__bd" style="height: 100%">
        <div class="weui-tab">
            <div class="weui-tab__panel" id="listData">

                <!-- <div class="weui-panel">
                    <div class="weui-panel__bd">
                        <div class="weui-media-box weui-media-box_text">
                            <h4 class="weui-media-box__title">鲍鲍, 13009722212</h4>
                            <p class="weui-media-box__desc">黑龙江哈尔滨市道里区松源街45号6单元603室</p>
                        </div>
                    </div>
                    <div class="weui-cells weui-cells_checkbox">
                        <div class="weui-cell weui-check__label">
                            <label class="weui-cell__bd">
                                <input type="radio" class="weui-check" name="radio" checked="checked">
                                <i class="weui-icon-checked"></i>
                                设为默认地址
                            </label>
                            <div class="weui-cell__ft">
                                <a class="weui-button weui-button_primary" href="{:url('Index/address_edit')}?id=1"><i class="fa fa-edit"></i> 编辑</a>
                                <span class="weui-button weui-button_warn"><i class="fa fa-trash"></i> 删除</span>
                            </div>
                        </div>
                    </div>
                </div> -->

            </div>
            <div class="weui-tabbar">
                <div class="weui-tabbar__item" style="padding-top: 0">
                    <a class="weui-btn weui-btn_warn" href="{:url('Index/address_add')}" style="border-radius: 0">新增收货地址</a>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
    $.ajax({
        url: '/api/v1/address/lists',
        type: 'GET',
        dataType: 'json',
        data:{
            type:1
        }
    })
    .done(function(json) {
        console.log("success");
        renderData("#listData",json.data);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
    // 渲染列表
    function renderData(_elment,_data){
        for(var i= 0; i<_data.length;i++){
            if(_data[i].ifdefault==1){
                var _ifdefault = 'checked="checked"';
            }else{
                var _ifdefault = '';
            }
            var _item =
            '<div class="weui-panel">\
                <div class="weui-panel__bd">\
                    <div class="weui-media-box weui-media-box_text">\
                        <h4 class="weui-media-box__title">'+_data[i].name+', '+_data[i].tel+'</h4>\
                        <p class="weui-media-box__desc">'+_data[i].addrvalue + _data[i].addr+'</p>\
                    </div>\
                </div>\
                <div class="weui-cells weui-cells_checkbox">\
                    <div class="weui-cell weui-check__label">\
                        <label class="weui-cell__bd">\
                            <input type="radio" class="weui-check" name="radio" '+_ifdefault+' value="'+_data[i].id+'">\
                            <i class="weui-icon-checked"></i> 设为默认地址\
                        </label>\
                        <div class="weui-cell__ft">\
                            <a class="weui-button weui-button_primary" href="{:url('Index/address_edit')}?id='+_data[i].id+'"><i class="fa fa-edit"></i> 编辑</a>\
                            <span class="weui-button weui-button_warn" data-id="'+_data[i].id+'"><i class="fa fa-trash"></i> 删除</span>\
                        </div>\
                    </div>\
                </div>\
            </div>';

            $(_elment).append(_item);
        }

        
        // 设置默认地址
        $("input[name=radio]").click(function(event) {
            /* Act on the event */
            $.ajax({
                url: '/api/v1/address/updatedef',
                type: 'post',
                dataType: 'json',
                data:{
                    id: $(this).val(),
                    type:1,
                    ifdefault: 1
                },
            })
            .done(function(json) {
                console.log("success");
                $.toast("更改成功");
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });
        });
        // 删除
        $(".weui-button_warn").click(function(event) {
            /* Act on the event */
            var $this =$(this);
            $.confirm("您确定要删除改记录?", "提示", function() {
                $.ajax({
                    url: '/api/v1/address/del',
                    type: 'post',
                    dataType: 'json',
                    data:{
                        id: $this.data("id")
                    },
                })
                .done(function(json) {
                    console.log("success");
                    $.toast("删除成功", function() {
                        window.location.href = "{:url('Index/address')}";
                    });
                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                });
            }, function() {
              //取消操作
            });
        });

    }
    

//重置浏览器回退链接
browserBack("home.html");
</script>
{/block}